﻿using Dispatcher.Core.Models;
using Dispatcher.Core.Providers;
using MiniFox.Components;
using MiniFox.Data.NativeClient;
using MiniFox.Utility;

namespace Dispatcher.Data
{
    [AutoResolve]
    public class UserDataProvider : DataProvider, IUserProvider
    {
        public UserDataProvider() : base("DispatcherContext")
        {

        }

        public T GetOpenAuthUser<T>(string authName, string userId) where T : class
        {
            var dataCommand = this.GetDataCommand("getOpenAuthUser");
            var result = dataCommand.ExecuteObjectQuery(new { authName, userId });
            if (result == null)
                return null;
            var userData = result["UserData"];
            if (userData == null)
                return null;

            var user = userData.ToString().ToJsonObject<T>();
            return user;
        }

        public UserModel GetUser(string userId)
        {
            var objectQuery = this.GetDataCommand("getUser");
            var user = objectQuery.ExecuteObjectQuery<UserModel>(new { userId });
            return user;
        }
    }
}
